﻿using Corina.DataContext.Service;
using Corina.Datamodel.DataObject;
using Corina.Datamodel.Entities;
using Corina.Enumerations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Corina.DataContext.Master
{
    public class IndustryMaster : MasterBase
    {
        public ResultInfo GetAllItems(string account,string password)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            ResultInfo result = new ResultInfo(resultValue);

            if(resultValue == ResultValue.Succeed)
            {
                var items = GetInstance<IndustryService>().GetAllItems(ConnectionString(DatabaseKeys.CORINA_BASE), true);
                result.Entities.AddRange(items);
            }

            return result;
        }

        public ResultInfo GetAvailableItems()
        {
            var items = GetInstance<IndustryService>().GetAllItems(ConnectionString(DatabaseKeys.CORINA_BASE));
            return new ResultInfo(ResultValue.Succeed, items.ToArray());
        }

        public ResultInfo Update(string account, string password, IndustryInfo entity, EntityOperation operation)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            if (resultValue == ResultValue.Succeed)
            {
                bool flag = GetInstance<IndustryService>().Update(ConnectionString(DatabaseKeys.CORINA_BASE), entity, operation);
                if (!flag)
                {
                    resultValue = ResultValue.Failed;
                }
            }

            ResultInfo result = new ResultInfo(resultValue);

            return result;
        }

        public ResultInfo ChangeAvailability(string account, string password, int id, bool availability)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            if (resultValue == ResultValue.Succeed)
            {
                bool flag = GetInstance<IndustryService>().ChangeAvailability(ConnectionString(DatabaseKeys.CORINA_BASE), id, availability);
                if (!flag)
                {
                    resultValue = ResultValue.Failed;
                }
            }

            ResultInfo result = new ResultInfo(resultValue);

            return result;
        }
    }
}
